<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入 vue.js 文件 -->
    <script src="../js/vue.js"></script>
</head>

<body>
    <!-- 父组件模板 -->
    <div id="app">
        <p>当前计数：{{total}}</p>
        <cpn @decrements="totalClick" @increments="totalClick"></cpn>
    </div>

    <!-- 子组件模板 -->
    <template id="cpn">
        <div>
            <button @click="decrement" :disabled="isBtn">-</button>
            <button @click="increment">+</button>
        </div>
    </template>

    <script>
        // 子组件
        const cpn1 = {
            // template绑定id
            template: '#cpn',
            data() {
                return {
                    counter: 0,
                    isBtn: false
                }
            },
            methods: {
                // 减
                decrement() {
                    if (this.counter === 0) {
                        this.isBtn = true;
                    } else {
                        this.counter--;
                        // 把--后的值传递给父组件的total
                        this.$emit("decrements", this.counter)
                    }
                },
                // 加
                increment() {
                    this.counter++;
                    // 把++后的值传递给父组件的total
                    this.isBtn = false;
                    this.$emit("increments", this.counter)
                }
            }
        }

        // 父组件
        const app = new Vue({
            el: "#app",
            data: {
                message: "Hello Vue",
                total: 0
            },
            components: {
                cpn: cpn1
            },
            // 通过事件接收子组件传递过来的数据
            methods: {
                totalClick(counter) {
                    // console.log(counter);
                    // 把子组件传递过来的counter赋值给total
                    this.total = counter;
                }
            }
        });
    </script>
</body>

</html>